package net.oschina.arvin.sqlbuilder.definition;

import net.oschina.arvin.sqlbuilder.WhereBuilder;
import net.oschina.arvin.sqlbuilder.enums.CompareType;
import net.oschina.arvin.sqlbuilder.exceptions.SqlBuilderException;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 范围查询
 *
 * @author Arvin
 * @time 2017/11/17 16:49
 */
public class RangeGeneralConditionItem extends AbstractGeneralConditionItem {

    private static Set<CompareType> supportCompareTypeSet = new HashSet<CompareType>(Arrays.asList(
            CompareType.BETWEEN
    ));

    /** 做开区间的值 */
    private final Object first;
    /** 右开区间的值 */
    private final Object second;

    public RangeGeneralConditionItem(ColumnDefinition modelColumnDefinition, Object first, Object second) {
        super(modelColumnDefinition, CompareType.BETWEEN);

        this.first = first;
        this.second = second;
    }


    @Override
    protected boolean isSupportCompareType(CompareType compareType) {
        return supportCompareTypeSet.contains(compareType);
    }

    @Override
    protected boolean customAppendToWhereBuilder(Object queryCondition, WhereBuilder whereBuilder, CompareType compareType) {

        String unWrapColumnName = getModelColumnDefinition().getUnWrapColumnName();

        switch (compareType) {
            case BETWEEN:
                whereBuilder.andBetween(unWrapColumnName, first, second);
                break;
            default:
                throw new SqlBuilderException("不支持的比较类型！");
        }

        return true;
    }
}
